//
//  HWGuanLianEmailViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/30.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWGuanLianEmailViewController.h"
#import "HWGuanlianPhoneTableViewCell.h"
#import "Countdown.h"
@interface HWGuanLianEmailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) UIView *lowerPartView;
@property(strong,nonatomic) UITextField *oneTextField;  // 第一个
@property(strong,nonatomic) UITextField *twoTextfeild;  //第二个
@property(strong,nonatomic) NSString *dataStr; // 验证码返回的参数
@end

@implementation HWGuanLianEmailViewController

-(UITableView *)mainaTableVie {

    if  (_mainaTableVie == nil) {

        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, CF_NavHeight+88) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
       // _mainaTableVie.backgroundColor = RGB(241, 242, 244);
        
    }
    return   _mainaTableVie ;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    
}


//设置UI
-(void)setupUI {
    self.navigationItem.title = @"关联邮箱";
    self.navigationItem.title = [HWjianzhiduiObject keylocalization:@"关联邮箱"];
    
    self.dataStr = @"";
    self.view.backgroundColor = [UIColor whiteColor];
   // self.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.mainaTableVie];

    [self addLowerPartView];
    
  
    

}


// 发送邮箱验证吗
- (void)guanlianyouxiang {
    

    
     NSString *userID = [HWLocallyStoredData getLoginUserID];
     NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
     NSString *signMD5 = [WMUtil md5HexDigest:sign];
     NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"bind_email_code",
                                @"ios" : @"1",
                                @"uid" :userID,
                                @"t" :tStr,
                                @"sign": signMD5,
                                @"email": self.oneTextField.text
                                };
    [MBProgressHUD show:@"正在发送验证码"];
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
      
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
     //   [MBProgressHUD hideHUD];
        
        
        if ([codeStr intValue] == 1) {
          
            //  response[@"data"];
            
          //  [MBProgressHUD showSuccess:@"验证码发送成功"];
            
             [MBProgressHUD  showSuccess:response[@"message"]];
            
            self.dataStr =  [NSString stringWithFormat:@"%@",response[@"data"]];
            
            
            

        } else {
            //  self.dataStr= @"";
             [MBProgressHUD  showError:response[@"message"]];
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
      
        [MBProgressHUD  showError:@"验证码发送失败"];
       // self.dataStr= @"";
        
    }];
    
    
    
}










// 添加低部UIview
- (void)addLowerPartView {
    
    self.lowerPartView = [[UIView alloc]initWithFrame:CGRectMake(0, CF_NavHeight+88+30, kScreenW, 44)];
    // self.lowerPartView.backgroundColor = [UIColor yellowColor];
    self.lowerPartView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.lowerPartView];
    NSString *banding =   [HWjianzhiduiObject keylocalization:@"绑定"];
    
    UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(15, 0, kScreenW-15*2, 36) Title:banding Image:nil Font:13 Color:RGB(255, 255, 155) Target:self Selector:@selector(buttonTap)];
   //  butt.backgroundColor = blueBackgroundBT;
    butt.layer.cornerRadius = 18;
    butt.layer.masksToBounds = YES;
    butt.backgroundColor = [UIColor bm_colorGradientChangeWithSizeView:butt];
    [self.lowerPartView addSubview:butt];
    
}

- (void)buttonTap {
    
    NSLog(@"保存");
//    HWbangdingQQViewController *bangding = [[HWbangdingQQViewController alloc]init];
//    [self.navigationController pushViewController:bangding animated:YES];
    
    if (!(self.oneTextField.text.length > 0)) {
           [MBProgressHUD showError:@"邮箱不能为空"];
           return;
    } else if (!(self.twoTextfeild.text.length > 0)) {
        
        
         [MBProgressHUD showError:@"验证码不能为空"];
          return;
        
        
    }
    
    
      NSString *userID = [HWLocallyStoredData getLoginUserID];
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
      NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
      NSString *signMD5 = [WMUtil md5HexDigest:sign];
      NSDictionary *parametes = @{
                                 @"ct" :  @"app",
                                 @"ac" : @"bind_email",
                                 @"ios" : @"1",
                                 @"code" : self.twoTextfeild.text,
                                 @"sessionid" : self.dataStr,
                                 @"uid" :userID,
                                 @"t" :tStr,
                                 @"sign": signMD5,
                                 @"email": self.oneTextField.text
                                 };
     
     [MBProgressHUD show:@"正在绑定邮箱"];
     
     [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
         
       
         NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
      //   [MBProgressHUD hideHUD];
         
         
         if ([codeStr intValue] == 1) {
           
       
            [MBProgressHUD  showSuccess:response[@"message"]];
             [self.navigationController popViewControllerAnimated:YES];
             
             
             

         } else {
          
              [MBProgressHUD  showSuccess:response[@"message"]];
             
         }
         NSLog(@"%@",response);
         
         
     } failure:^(NSString *error) {

         [MBProgressHUD  showError:@"绑定邮箱成功"];
        
     }];
     
    
    
    
       
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    static NSString *cellID = @"cellID";
    
    HWGuanlianPhoneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[HWGuanlianPhoneTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    
    if (indexPath.row == 0) {
        
        cell.namelabel.text = @"邮箱号码";
        cell.namelabel.text = [HWjianzhiduiObject keylocalization:@"邮箱号码"];
        
        cell.codeBut.hidden = YES;
        
        NSString *shuru = [HWjianzhiduiObject keylocalization:@"请输入邮箱号码"];
        
        
       NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:shuru attributes:
                   @{NSForegroundColorAttributeName:RGB(153, 153, 153),
                     NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
                   ];
      cell.titleTextField.attributedPlaceholder  = attrString;
        self.oneTextField = cell.titleTextField;
        
        
    } else if (indexPath.row == 1) {
        
         cell.namelabel.text = @"验证码";
        cell.namelabel.text = [HWjianzhiduiObject keylocalization:@"验证码"];
         cell.codeBut.hidden = NO;
        NSString *yanzhengshuru = [HWjianzhiduiObject keylocalization:@"请输入验证码"];
         NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yanzhengshuru attributes:
                         @{NSForegroundColorAttributeName:RGB(153, 153, 153),
                           NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
                         ];
         cell.titleTextField.attributedPlaceholder  = attrString;
        
          self.twoTextfeild = cell.titleTextField;
        
        
    }
    
    [cell.codeBut addTarget:self action:@selector(xianshyanjTap:) forControlEvents:UIControlEventTouchUpInside];

    return  cell;
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return  44;
    
}





// 获取验证码
- (void)xianshyanjTap:(UIButton *)sende {
    
//    if (![WMUtil validateMobile:self.titleTextField.text]) {
//
//        [MBProgressHUD showError:@"请输入正确的手机号"];
//        return ;
//
//
//    }
//
    
    
    

    
    
    
    [Countdown  StartTimeCodeBtn:sende];
    if (!(self.oneTextField.text.length > 0)) {
        [MBProgressHUD showError:@"邮箱不能为空"];
        return;
    }
   
    
    [self guanlianyouxiang];
    
    DLog(@"获取验证码");
    

    
}




@end
